package com.ruoyi.system.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.mapper.AgoniVirtualStoreMapper;
import com.ruoyi.system.pojo.entity.VirtualStore;
import com.ruoyi.system.pojo.vo.StoreScoreVO;
import com.ruoyi.system.service.IAgoniRankingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class IAgoniRankingServiceImpl implements IAgoniRankingService {

    @Autowired
    private AgoniVirtualStoreMapper agoniVirtualStoreMapper;


    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<StoreScoreVO> getStoreRank() {  List<StoreScoreVO> storeScores = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();

        // 使用 SCAN 替代 KEYS 以避免性能问题
        Set<String> keys = redisTemplate.keys("store_score:*");

        if (keys == null || keys.isEmpty()) {
            return storeScores; // 如果 keys 为空，直接返回空列表
        }

        for (String key : keys) {
            String storeScoreJson = String.valueOf(redisTemplate.opsForValue().get(key));
            if (storeScoreJson != null) {
                try {
                    StoreScoreVO storeScoreVO = objectMapper.readValue(storeScoreJson, StoreScoreVO.class);
                    storeScores.add(storeScoreVO);
                } catch (JsonProcessingException e) {
                    // 记录日志或处理异常
                    log.error("反序列化失败: " + storeScoreJson, e);
                }
            }
        }

        // 使用 Stream API 进行排序
        storeScores.sort(Comparator.comparingDouble(StoreScoreVO::getScore).reversed());

        log.info("获取商店排行榜成功: {}", storeScores);
        return storeScores;
    }

    @PostConstruct
    public void initRanking() {
        // TODO: 初始化排行榜数据
        System.out.println("初始化排行榜数据");
        List<VirtualStore> virtualStores = agoniVirtualStoreMapper.selectVirtualStoreList();

        List<StoreScoreVO> storeScoreVOS = new ArrayList<>();

        for (VirtualStore virtualStore : virtualStores) {
            System.out.println(virtualStore.toString());
            StoreScoreVO storeScoreVO = new StoreScoreVO();
            storeScoreVO.setStoreId(Long.valueOf(virtualStore.getId()));
            storeScoreVO.setStoreName(virtualStore.getName());
            storeScoreVO.setScore(virtualStore.getRating());
            storeScoreVO.setStoreScoreNum(1);


            storeScoreVOS.add(storeScoreVO);
        }
        // 将数据写入 Redis
        for (StoreScoreVO storeScoreVO : storeScoreVOS) {
            // 使用商店 ID 作为 Key，将整个 StoreScoreVO 对象存储
            //序列化对象
            ObjectMapper mapper = new ObjectMapper();
            String jsonStr = null;
            try {
                jsonStr = mapper.writeValueAsString(storeScoreVO);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            redisTemplate.opsForValue().set("store_score:" + storeScoreVO.getStoreId(), jsonStr);
        }
        System.out.println("初始化排行榜数据完成");

    }


}
